package com.goods.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.entities.Supplier;
import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.SupplierService;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Decheng Kong
 * @date 2021/11/8 17:47
 */
@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private SupplierMapper supplierMapper;

    @Override
    public PageVO<SupplierVO> findSupplierList(Integer pageNum, Integer pageSize, String name, String address, String contact) {
        PageHelper.startPage(pageNum, pageSize);

        Example example = new Example(Supplier.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(name)) {
            criteria.andLike("name", "%" + name + "%");
        }

        if (!StringUtils.isEmpty(address)) {
            criteria.andLike("address", "%" + address + "%");
        }

        if (!StringUtils.isEmpty(contact)) {
            criteria.andLike("contact", "%" + contact + "%");
        }

        List<Supplier> suppliers = supplierMapper.selectByExample(example);
        List<SupplierVO> collect = suppliers.stream().map(supplier -> {
            SupplierVO supplierVO = new SupplierVO();
            BeanUtils.copyProperties(supplier, supplierVO);
            return supplierVO;
        }).collect(Collectors.toList());

        PageInfo<Supplier> info = new PageInfo<>(suppliers);
        return new PageVO<>(info.getTotal(), collect);
    }

    @Override
    public void addSupplier(SupplierVO supplierVO) {
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierVO, supplier);
        supplier.setCreateTime(new Date());
        supplierMapper.insert(supplier);
    }

    @Override
    public SupplierVO getSupplierById(Integer id) {

        Supplier supplier = supplierMapper.selectByPrimaryKey(id);
        SupplierVO supplierVO = new SupplierVO();
        BeanUtils.copyProperties(supplier, supplierVO);
        return supplierVO;
    }

    @Override
    public void updateSupplier(SupplierVO supplierVO) {
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierVO, supplier);
        supplier.setModifiedTime(new Date());
        supplierMapper.updateByPrimaryKey(supplier);
    }

    @Override
    public void deleteSupplier(Integer id) {
        supplierMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<SupplierVO> findSupplierList() {

        List<Supplier> suppliers = supplierMapper.selectAll();
        List<SupplierVO> collect = suppliers.stream().map(supplier -> {
            SupplierVO supplierVO = new SupplierVO();
            BeanUtils.copyProperties(supplier, supplierVO);
            return supplierVO;
        }).collect(Collectors.toList());

        return collect;

    }
}
